﻿/// <reference path="jquery-1.3.2.min-vsdoc.js" />
/// <reference path="MG.Core-vsdoc.js" />

(function() {

    var loadRss = function(feedUrl, callback) {
        var url = '/Data/Rss?feedUrl=' + encodeURI(feedUrl);
        $.getJSON(url, null, callback);
    };

    var loadCalendar = function(feedUrl, callback) {
        var url = '/Data/Calendar?feedUrl=' + encodeURI(feedUrl);
        $.getJSON(url, null, callback);
    };

    var loadGroup = function(feedUrl, callback) {
        var url = '/Data/Group?feedUrl=' + encodeURI(feedUrl);
        $.getJSON(url, null, callback);
    };

    var loadGroups = function(feedUrl, callback) {
        var url = '/Data/Groups?feedUrl=' + encodeURI(feedUrl);
        $.getJSON(url, null, callback);
    };

    var getDate = function(jsonDate) {
        return eval(jsonDate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
    };

    var getFormattedDate = function(date) {
        return (date.getMonth() + 1) + '/' +
            date.getDate() + '/' +
            (date.getYear() + 1900);
    };

    var getFormattedTime = function(date) {
        var a_p = "";
        var curr_hour = date.getHours();
        if (curr_hour < 12) {
            a_p = "AM";
        }
        else {
            a_p = "PM";
        }
        if (curr_hour === 0) {
            curr_hour = 12;
        }
        if (curr_hour > 12) {
            curr_hour = curr_hour - 12;
        }

        var curr_min = date.getMinutes();

        curr_min = curr_min + "";

        if (curr_min.length == 1) {
            curr_min = "0" + curr_min;
        }

        return (curr_hour + ":" + curr_min + " " + a_p);
    };

    if (window['MG'] === undefined) {
        window['MG'] = {};
    }

    MG.loadRss = loadRss;
    MG.loadCalendar = loadCalendar;
    MG.loadGroup = loadGroup;
    MG.loadGroups = loadGroups;
    MG.getDate = getDate;
    MG.getFormattedDate = getFormattedDate;
    MG.getFormattedTime = getFormattedTime;

} ());
